<template>
    <div class="layout" :class="{'layout-hide-text': spanLeft < 4}">
        <Row type="flex">
            <i-col :span="spanLeft" class="layout-menu-left" v-show="showMenu">
                <Menu active-name="/" theme="dark" width="auto" :open-names="[]" accordion
                      @on-select="onMenuSelect">
                    <div class="layout-logo-left"></div>
                    <Menu-item name="/">
                        <Icon type="ios-speedometer" :size="iconSize"></Icon>
                        <span class="layout-text">控制台</span>
                    </Menu-item>
                    <Submenu name="1">
                        <template slot="title">
                            <Icon type="ios-navigate" :size="iconSize"></Icon>
                            <span class="layout-text">名校名师</span>
                        </template>
                        <Menu-item name="/famous-school">名校管理</Menu-item>
                        <Menu-item name="/famous-teacher">名师管理</Menu-item>
                    </Submenu>
                    <Submenu name="2">
                        <template slot="title">
                            <Icon type="person" :size="iconSize"></Icon>
                            <span class="layout-text">前台用户管理</span>
                        </template>
                        <Menu-item name="/student">学生管理</Menu-item>
                        <Menu-item name="/teacher">教师管理</Menu-item>
                        <Menu-item name="/company">社会用户管理</Menu-item>
                    </Submenu>
                    <Submenu name="3">
                        <template slot="title">
                            <Icon type="ios-analytics" :size="iconSize"></Icon>
                            <span class="layout-text">赛事相关</span>
                        </template>
                        <Menu-item name="/award">赛事管理</Menu-item>
                        <Menu-item name="/proposition">命题管理</Menu-item>
                        <Menu-item name="/works-type">作品类别管理</Menu-item>
                    </Submenu>
                </Menu>
            </i-col>
            <i-col :span="spanRight">
                <div :style="'min-height:' + layoutMinHeight + 'px;'">
                    <div class="layout-header">
                        <i-button type="text" @click="toggleClick">
                            <Icon type="navicon" size="32"></Icon>
                        </i-button>
                        <i-button type="text" class="pull-right" style="margin-top: 5px" @click="logout">
                            <Icon type="log-out" size="16"></Icon>
                            <span style="font-size: 14px">登出</span>
                        </i-button>
                    </div>
                    <div class="layout-breadcrumb">
                        <Breadcrumb>
                            <Breadcrumb-item href="/" v-if="breadCrumbs.length > 0">首页</Breadcrumb-item>
                            <template v-for="item in breadCrumbs">
                                <Breadcrumb-item v-if="item.active == true">{{item.name}}</Breadcrumb-item>
                                <Breadcrumb-item v-else :href="item.href">{{item.name}}</Breadcrumb-item>
                            </template>
                        </Breadcrumb>
                    </div>
                    <div class="layout-content" :style="'min-height:' + layoutContentMinHeight + 'px;'">
                        <div class="layout-content-main">
                            <transition name="slide-fade">
                                <router-view></router-view>
                            </transition>
                        </div>
                    </div>
                    <div class="layout-copy">
                        2011-2016 &copy; Bright-tech
                    </div>
                </div>

            </i-col>
        </Row>
    </div>
</template>
<script>
    export default {
        name: 'app',
        data () {
            return {
                spanLeft: 4,
                spanRight: 20,
                showMenu: true
            }
        },
        created: function () {
            this.$store.dispatch('fetchUser');
            this.$store.dispatch('fetchUserPermissions');
        },
        computed: {
            iconSize () {
                return this.spanLeft === 4 ? 14 : 24;
            },
            layoutMinHeight () {
                return window.innerHeight;
            },
            layoutContentMinHeight () {
                let layoutContentMinHeight = window.innerHeight - 175;
                return layoutContentMinHeight;
            },
            breadCrumbs (){
                return this.$store.state.breadCrumbs;
            }
        },
        methods: {
            onMenuSelect (location){
                console.log(location);
                this.$router.push(location);
            },
            logout(){
                axios.post('logout', {})
                    .then(function (response) {
                        window.location.reload();
                    })
                    .catch(function (error) {
                        console.log(error);
                    });
            },
            toggleClick () {
                if (this.spanLeft === 4) {
                    if (window.innerWidth < 768){
                        this.showMenu = false;
                        this.spanRight = 24;
                        this.spanLeft = 0;
                    }else{
                        this.spanRight = 23;
                        this.spanLeft = 1;
                        this.showMenu = true;
                    }
                } else {
                    this.showMenu = true;
                    this.spanLeft = 4;
                    this.spanRight = 20;
                }
            }
        }
    }
</script>
<style scoped>
    .layout{
        background: #f5f7f9;
        position: relative;
        overflow: hidden;
    }
    .layout-breadcrumb{
        padding: 10px 15px 0;
    }
    .layout-content{
        margin: 15px;
        overflow: hidden;
        background: #fff;
        border-radius: 4px;
    }
    .layout-content-main{
        padding: 10px;
    }
    .layout-copy{
        text-align: center;
        padding: 10px 0 20px;
        color: #9ea7b4;
    }
    .layout-menu-left{
        background: #464c5b;
    }
    .layout-header{
        height: 60px;
        background: #fff;
        box-shadow: 0 1px 1px rgba(0,0,0,.1);
    }
    .layout-logo-left{
        width: 90%;
        height: 30px;
        background: #5b6270;
        border-radius: 3px;
        margin: 15px auto;
    }
    .layout-ceiling-main a{
        color: #9ba7b5;
    }
    .layout-hide-text .layout-text{
        display: none;
    }
    .ivu-col{
        transition: width .2s ease-in-out;
    }
    /* 可以设置不同的进入和离开动画 */
    /* 设置持续时间和动画函数 */
    .slide-fade-enter-active {
        transition: all .3s ease;
    }
    .slide-fade-leave-active {
        transition: all .3s cubic-bezier(1.0, 0.5, 0.8, 1.0);
    }
    .slide-fade-enter, .slide-fade-leave-to
        /* .slide-fade-leave-active for below version 2.1.8 */ {
        transform: translateX(10px);
        opacity: 0;
    }
</style>